# coding: utf8
import sys, os


session.amazonproduct_installed = True

if session.amazonproduct_installed == True:
    module_path = os.path.join(request.folder, 'modules')
    private_path = os.path.join(request.folder, 'private')
    if not module_path in sys.path:
        sys.path.append(module_path)
    try:
        import lmsconfig
        session._lmsconfig_installed = True
        cfg = lmsconfig.getcfg(private_path)
    except ImportError:
        session._lmsconfig_installed = False

#module_path = os.path.join(request.folder, 'modules')
#private_path = os.path.join(request.folder, 'private')
#if not module_path in sys.path:
#    sys.path.append(module_path)
#import lmsconfig
#cfg = lmsconfig.getcfg(module_path)

db = DAL(cfg.dbpath)
#db = DAL('sqlite://storage.sqlite')       # use SQLite or other DB


#db = DAL('postgres://libuser:pswd@localhost/lmsweb') # sample connection string for postgres
db = DAL('sqlite://storage.sqlite')       # use SQLite or other DB

from gluon.tools import *
# The below lines are commented out to disable authentication completely.  If you want to turn authentication on,
# just uncomment them, and then uncomment @auth decorators in appadmin.py and default.py as needed.
#auth=Auth(globals(),db)                      # authentication/authorization
#auth.settings.hmac_key='sha512:32b6c31c-d643-408f-8415-e7e07efb91e0'
#auth.settings.actions_disabled.append('register') # block user registration, only admins can add new users
#auth.settings.actions_disabled.append('profile')  # block access to profiles, as they are not required
#auth.settings.actions_disabled.append('retrieve_username') # block access to username retrieval
#auth.settings.actions_disabled.append('retrieve_password') # block access to password retrieval
#auth_table =db.define_table(auth.settings.table_user_name, # custom user table
#    Field('first_name', length=128, default=''),
#    Field('last_name', length=128, default=''),
#    Field('username', length=128, default=''),
#    Field('email', unique=True, length=128, default=''),
#    Field('password', 'password', length=256, default='', readable=False, label='Password'),
#    Field('registration_key', length=128, readable=False, writable=False))
#auth_table.first_name.requires = IS_NOT_EMPTY(error_message=auth.messages.is_empty) #validators req'd by web2py
#auth_table.last_name.requires = IS_NOT_EMPTY(error_message=auth.messages.is_empty)
#auth_table.username.requires = IS_NOT_IN_DB(db, auth_table.username)
#auth_table.password.requires = [IS_STRONG(), CRYPT()]
#auth_table.email.requires = [IS_EMAIL(error_message=auth.messages.invalid_email), IS_NOT_IN_DB(db, auth_table.email)]
#auth.settings.table_user = auth_table # set above table definition as user table for db
#auth.define_tables()                         # creates all needed tables
#crud=Crud(globals(),db)                      # for CRUD helpers using auth

# the below are for people that use internal mail servers and want to set up email-based auth.
# crud.settings.auth=auth                      # enforces authorization on crud
# mail=Mail()                                  # mailer
# mail.settings.server='smtp.gmail.com:587'    # your SMTP server
# mail.settings.sender='you@gmail.com'         # your email
# mail.settings.login='username:password'      # your credentials or None
# auth.settings.mailer=mail                    # for user email verification
# auth.settings.registration_requires_verification = True
# auth.settings.registration_requires_approval = True
# auth.messages.verify_email = \
#  'Click on the link http://.../user/verify_email/%(key)s to verify your email'
## more options discussed in gluon/tools.py
#########################################################################
from gluon.tools import *
mail = Mail()                                  # mailer
auth = Auth(globals(),db)                      # authentication/authorization
crud = Crud(globals(),db)                      # for CRUD helpers using auth
service = Service(globals())                   # for json, xml, jsonrpc, xmlrpc, amfrpc
plugins = PluginManager()

mail.settings.server = 'logging' or 'smtp.gmail.com:587'  # your SMTP server
mail.settings.sender = 'you@gmail.com'         # your email
mail.settings.login = 'username:password'      # your credentials or None

auth.settings.hmac_key = '<your secret key>'   # before define_tables()
auth.define_tables()                           # creates all needed tables
auth.settings.mailer = mail                    # for user email verification
auth.settings.registration_requires_verification = False
auth.settings.registration_requires_approval = False
auth.messages.verify_email = 'Click on the link http://'+request.env.http_host+URL('default','user',args=['verify_email'])+'/%(key)s to verify your email'
auth.settings.reset_password_requires_verification = True
auth.messages.reset_password = 'Click on the link http://'+request.env.http_host+URL('default','user',args=['reset_password'])+'/%(key)s to reset your password'

#########################################################################
## If you need to use OpenID, Facebook, MySpace, Twitter, Linkedin, etc.
## register with janrain.com, uncomment and customize following
# from gluon.contrib.login_methods.rpx_account import RPXAccount
# auth.settings.actions_disabled=['register','change_password','request_reset_password']
# auth.settings.login_form = RPXAccount(request, api_key='...',domain='...',
#    url = "http://localhost:8000/%s/default/user/login" % request.application)
## other login methods are in gluon/contrib/login_methods
#########################################################################

crud.settings.auth = None                      # =auth to enforce authorization on crud

#########################################################################
## Define your tables below (or better in another model file) for example
##
## >>> db.define_table('mytable',Field('myfield','string'))
##
## Fields can be 'string','text','password','integer','double','boolean'
##       'date','time','datetime','blob','upload', 'reference TABLENAME'
## There is an implicit 'id integer autoincrement' field
## Consult manual for more options, validators, etc.
##
## More API examples for controllers:
##
## >>> db.mytable.insert(myfield='value')
## >>> rows=db(db.mytable.myfield=='value').select(db.mytable.ALL)
## >>> for row in rows: print row.id, row.myfield
#######################
